package com.song.tree;

/**
 *  二分查找
 */
public class BinSearch {

        public int binSearch(int[] a , int key , int low , int high) {

            int mid = (low + high ) /2 ;
            while (low <= high ) {
                if (a[mid] == key ) {
                    return mid;
                }
                if (a[mid] > key ) {
                    high = mid -1 ;
                    mid = (low + high ) / 2;
                }
                if (a[mid] < key ) {
                    low = mid + 1;
                    mid = (low + high ) / 2;
                }

            }

            return -1;
        }

        public static void main(String[] args) {

            int[] a = {1,2,3,4,5,6,9};
            BinSearch binSerach = new BinSearch();

            int binSearchs = binSerach.binSearch(a, 3, 0, a.length - 1);
            System.out.println(binSearchs);
        }


}
